package com.linktrip.devicemanager.filter;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.util.DES;
import com.linktrip.devicemanager.util.ToolKits;
import sun.misc.BASE64Decoder;

public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	Logger log = LoggerFactory.getLogger(EncryptablePropertyPlaceholderConfigurer.class);

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		// TODO Auto-generated method stub
		// super.processProperties(beanFactoryToProcess, props);
		String driverClassName = props.getProperty(Constant.JDBC_DRIVERCLASSNAME);
		if (!ToolKits.isEmpty(driverClassName)) {
			props.setProperty(Constant.JDBC_DRIVERCLASSNAME, getDeCodeString(driverClassName));
		}

		String url = props.getProperty(Constant.JDBC_URL);
		if (!ToolKits.isEmpty(url)) {
			props.setProperty(Constant.JDBC_URL, getDeCodeString(url));
		}

		String username = props.getProperty(Constant.JDBC_USERNAME);
		if (!ToolKits.isEmpty(username)) {
			props.setProperty(Constant.JDBC_USERNAME, getDeCodeString(username));
		}

		String password = props.getProperty(Constant.JDBC_PASSWORD);
		if (!ToolKits.isEmpty(password)) {
			props.setProperty(Constant.JDBC_PASSWORD, getDeCodeString(password));
		}
		
		super.processProperties(beanFactoryToProcess, props);

	}

	@SuppressWarnings("restriction")
	public String getDeCodeString(String data) {
		String decode = "";
		try {
			decode = new String(
					DES.des3DecodeCBC(Constant.DES_KEY, Constant.DES_KEY_IV, new BASE64Decoder().decodeBuffer(data)),
					"UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return decode;
	}
}
